function PriorityQueue(options) {
	var contents = [];
	var isSorted = false;
	var comparingFunction = (options && options.cmpFunc) ? options.cmpFunc
			: autoCompare;

	var autoCompare = function(a, b) {
		return a - b;
	};

	var sort = function() {		
		contents.sort(comparingFunction);
		isSorted = true;	
	};

	this.adjust = function() {
		sort();
	};

	this.isEmpty = function() {
		return contents.length === 0;
	};

	this.getSize = function() {
		return contents.length;
	};

	this.isIncluded = function(element) {
		for ( var i = 0; i < contents.length; i++) {
			if (contents[i] === element) {
				return true;
			}
		}
		return false;
	};
	
	this.indexOf = function(element) {
		for ( var i = 0; i < contents.length; i++) {
			if (contents[i] === element) {
				return i;
			}
		}
		return -1;
	};		

	this.top = function() {
		if (this.isEmpty()) {
			return undefined;
		} else {
			if(isSorted !== true){
				sort();
			}
			return contents[this.getSize() - 1];
		}
	};	

	this.pop = function() {
		if (this.isEmpty()) {
			return undefined;
		} else {
			if(isSorted !== true){
				sort();
			}
			return contents.pop();
		}
	};
	
	this.remove = function(element) {
		var i = this.indexOf(element);
		if(i > -1){
			contents.splice(i, 1);
			return true;
		} else {
			return false;
		}
	};
	
	this.removeAt = function(index) {
		if(index >= 0 && index < contents.length){
			contents.splice(index, 1);
			return true;
		} else {
			return false;
		}
	};
	
	this.clear = function() {
		while(contents.length > 0){
			contents.pop();
		}
	}

	this.push = function(newElement) {		
		contents.push(newElement);
		isSorted = false;
	};

	var documentWriteLn = function(x) {
		document.writeln(x);
	};
	this.displayOne = documentWriteLn;
	this.display = function() {
		if(isSorted !== true){
			sort();
		}
		for ( var i = contents.length-1; i >= 0; --i) { 
			this.displayOne(contents[i]);
		}
	};
	
	var iterator;
	this.beginIterating = function(){
		iterator = contents.length - 1;
		return contents[iterator];
	};
	
	this.isEndIterating = function(){
		return (iterator == -1);
	};
	
	this.nextElement = function(){
		if(iterator > 0){
			return contents[--iterator];
		} else {
			iterator = -1;
			return undefined;
		}
	}

	return this;
}